// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Abp;
using Abp.Castle.Logging.Log4Net;
using Abp.Dependency;
using Castle.Facilities.Logging;
using GCT.Foundation.EntityFrameworkCore;
using GCT.Foundation.Identity;
using L._52Abp;
using Microsoft.Extensions.DependencyInjection;

namespace GCT.Foundation
{
    public class Startup : I52AbpStartup
    {
        private readonly MigratorOptions _options;

        public Startup(MigratorOptions options)
        {
            _options = options;
        }

        public void ConfigurationServices(IServiceCollection services)
        {
            
            // identity
            services.AddAppIdentity();
            // type finder
            services.Add52AbpTypeFinder((options) =>
            {
                // skip find
                options.AddSkip<FoundationDbContext>();
            });
        }

        public void ConfigureAbp(AbpBootstrapperOptions options)
        {
            options.IocManager.IocContainer.AddFacility<LoggingFacility>(f =>
            {
                f.UseAbpLog4Net().WithConfig("log4net.config");
            });
        }

        public void Configure(IServiceProvider serviceProvider)
        {
            using (var abpBootstrapper = serviceProvider.GetService<AbpBootstrapper>())
            {
                abpBootstrapper.Initialize();
                using (var migrateExecuter = abpBootstrapper.IocManager
                           .ResolveAsDisposable<MultiTenantMigrateExecuter>())
                {
                    var migrationSucceeded = migrateExecuter.Object.Run(_options.QuietMode);
                }
            }
            Console.WriteLine("迁移执行结束...");
        }
    }
}
